#!/usr/bin/env perl
###########################################################################
#
#   Copyright 2010 American Public Media Group
#
#   This file is part of AIR2.
#
#   AIR2 is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   AIR2 is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
#
###########################################################################

use strict;
use warnings;
use Test::More tests => 12;
use lib 'tests/search';
use Data::Dump qw( dump );
use AIR2TestUtils;
use AIR2::Config;
use AIR2Test::Source;
use AIR2Test::SrcActivity;
use AIR2Test::Project;
use AIR2Test::SrcResponseSet;
use AIR2Test::Inquiry;

my $debug         = $ENV{PERL_DEBUG} || 0;
my $TEST_USERNAME = 'ima-test-user';
my $TEST_PROJECT  = 'ima-test-project';

$Rose::DB::Object::Debug          = $debug;
$Rose::DB::Object::Manager::Debug = $debug;

###################################
##           source
###################################

ok( my $source = AIR2Test::Source->new(
        src_username => $TEST_USERNAME,
        debug        => $debug
    ),
    "new source"
);
ok( $source->add_emails(
        [ { sem_email => $TEST_USERNAME . '@nosuchemail.org' } ]
    ),
    "add email address"
);

# use load_or_insert in case we aborted on earlier run and left data behind
ok( $source->load_or_insert(), "save source" );

# make sure our upd_dtim is current
ok( $source->src_upd_dtim( time() ), "set src_upd_dtim" );
ok( $source->save(),                 "save source with upd_dtim" );

ok( my $src_ids = $source->requires_indexing_ids( $source->src_upd_dtim() ),
    "get requires_indexing_id" );
is( scalar @$src_ids, 1, "one found for indexing" );

SKIP: {

    my $total = Rose::DB::Object::Manager->get_objects_count(
        'object_class' => 'AIR2::SrcActivity' );
    skip "No SrcActivity found in db", 2 if !$total;

    # touch a random activity and make sure it gets noticed
    my $rand_sact = AIR2::SrcActivity->fetch_all_iterator( limit => 1 )->next;
    $rand_sact->sact_upd_dtim( time() );
    $rand_sact->save();

    ok( $src_ids = $source->requires_indexing_ids( $source->src_upd_dtim() ),
        "get requires_indexing_id"
    );
    is( scalar @$src_ids, 2, "two found for indexing" );
}

###################################
##           project
###################################

ok( my $project = AIR2Test::Project->new( prj_name => $TEST_PROJECT ),
    "new project" );
ok( $project->load_or_insert, "save project" );
ok( my $prj_ids
        = AIR2Test::Project->requires_indexing_ids( $project->prj_upd_dtim ),
    "get requires_indexing_ids for Project"
);

#diag( dump( $prj_ids ));
